Imports System.Xml.Serialization
Imports system.io
Imports System.Runtime.Serialization
Public Class GlyphDB
    Private GlyphXmlFilename As String = "GlyphDB.xml"
    Private ArmoryDataPath As String = "..\..\ArmoryData"

    Public Structure Glyph
        <XmlAttribute()> Dim ID As Long
        <XmlAttribute()> Dim Name As String
        <XmlAttribute()> Dim Type As String
    End Structure

    Public Shared Glyphs() As Glyph

    Public Sub New()
        If Glyphs Is Nothing Then
            ReDim Glyphs(0)
            Glyphs(0) = New Glyph
            Glyphs(0).ID = 0
            Glyphs(0).Name = "none"
            Glyphs(0).Type = ""

            Dim filename As String
            filename = My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData, ArmoryDataPath), GlyphXmlFilename)

            Dim XU As New XmlUtils.AccessXml
            Try
                XU.ImportFromXml(Glyphs, filename)
            Catch ex As Exception
            End Try
        End If
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

    Public Sub StoreGlyphDB()
        Dim filename As String
        filename = My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData, ArmoryDataPath), GlyphXmlFilename)

        Dim XU As New XmlUtils.AccessXml
        Try
            XU.ExportByXml(Glyphs, filename)
        Catch ex As Exception
        End Try
    End Sub

    Public Sub SortGlyphDB()
        If Glyphs Is Nothing Then Exit Sub
        If Glyphs.Length < 2 Then Exit Sub

        Dim compare As New CompareGlyphDB
        Array.Sort(Glyphs, compare)
    End Sub

    Public Sub AddGlyphInfo(ByVal GlyphID As Long, ByVal GlyphType As String, ByVal GlyphName As String)
        Dim ct As Long = 0
        If Glyphs Is Nothing Then
            ct = 0
        ElseIf Glyphs.Length = 0 Then
            ct = 0
        Else
            Dim i As Long
            For i = 0 To Glyphs.Length - 1
                If Glyphs(i).ID = GlyphID Then
                    Exit Sub
                End If
            Next
            ct = Glyphs.Length
        End If

        ReDim Preserve Glyphs(ct)
        Glyphs(ct) = New Glyph

        Glyphs(ct).ID = GlyphID
        Glyphs(ct).Name = GlyphName
        Glyphs(ct).Type = GlyphType

        SortGlyphDB()
    End Sub

    Public Function GetGlyphInfo(ByVal GlyphID As Long) As Glyph
        Dim ct As Long = 0
        If Glyphs Is Nothing Then
            ct = 0
        ElseIf Glyphs.Length = 0 Then
            ct = 0
        Else
            Dim i As Long
            For i = 0 To Glyphs.Length - 1
                If Glyphs(i).ID = GlyphID Then
                    Return Glyphs(i)
                End If
            Next
            ct = 0
        End If

        Return Glyphs(ct)
    End Function
End Class


Public Class CompareGlyphDB
    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim d1 As GlyphDB.Glyph = CType(x, GlyphDB.Glyph)
        Dim d2 As GlyphDB.Glyph = CType(y, GlyphDB.Glyph)

        If d1.Type < d2.Type Then
            Return -1
        ElseIf d1.Type > d2.Type Then
            Return 1
        ElseIf d1.ID < d2.ID Then
            Return -1
        ElseIf d1.ID > d2.ID Then
            Return 1
        End If

        Return 0
    End Function
End Class


